<?php
//商品管理基本方法
//管理员、企业必须继承此类实现私有业务
abstract class module_product{
	
	//构造方法，接收url参数进行分发
	function __construct(){
		global $tp;
		global $act;
		global $company;
		$this->$act();
		$tp->assign('nav','商品管理');
	}
	
	//显示商品列表
	protected function index(){
		global $tp;
		global $Db;
		global $Base;
		global $company;
		$pageSize = 10;
		$page = intval($_REQUEST['page']);
		$page = $page<1 ? 1 : $page;
		$c_id = intval($_REQUEST['c_id']);	//商家id
		$cat_id = intval($_REQUEST['cat_id']);	//分类id
		$is_not = intval($_REQUEST['is_not']);
		
		//如果是商家，强制赋值$c_id
		if(!$company['is_admin'])	$c_id = $company['p_id']? $company['p_id']: $company['id'];
		
		//提取该商家的商品分类
		if($c_id>0){
			$product_cat = $this->get_company_product_cat($c_id);
			//print_r($product_cat);
			$tp->assign('product_cat',$product_cat);
		}
		
		$products = $this->get_product_list($c_id,$cat_id,$is_not,$page,$pageSize);
		//print_r($products);
		$tp->assign('products',$products);
		
		$pages = Common::set_page($pageSize,$page,$products['count']);
		foreach ($pages as $key => $value) {
			 $pages[$key]['url'] = "/".COMPANY_PATH."/?m=product&page=".$value['num'].$products['spm'];
		}
		
		$tp->assign("page",$page);
		$tp->assign("size",$pageSize);
		$tp->assign("pages",$pages);
	}
	
	//添加、编辑商品
	protected function detail(){
		global $Json;
		global $tp;
		global $Db;
		global $Base;
		global $company;

		$id = intval($_REQUEST['id']);  //当前操作的product商品id
		
		//判断$id是否存在
		$product = $this->check_product_exists($id);
		
		//判断非法操作
		if(empty($product)){
			$id = 0;
		}
		
		//显示数据
		if(empty($_POST)){
			//提取商品分类，商家提取自己的分类；管理员提取商品所属商家的分类
			$c_id = $product['c_id'];
			if(!$c_id) $c_id = $company['p_id'] ? $company['p_id'] : $company['id'];			
			$tp->assign('product_cat',$this->get_company_product_cat($c_id));
			
			$tp->assign('product',$product);
		}
		//数据操作
		else{
			$res['status'] = 0;
			$data['name'] = Common::sfilter($_POST['name']);
			$data['cat_id'] = intval($_POST['cat_id']);
			$data['describe'] = Common::sfilter($_POST['descript'],'html');
			$data['img_path'] = Common::sfilter($_POST['product_path']);
			$data['price'] = floatval($_POST['price']);
			$data['num'] = intval($_POST['num']);
			$data['sort'] = intval($_POST['sort']);
			$data['original_price'] = floatval($_POST['original_price']);
			$data['pano_url'] = Common::sfilter($_POST['pano_url']);
				
			// var_dump($data);die;
			if(empty($data['name'])){
				$res['msg'] = '商品名称不能为空！';
			}
			else if($data['cat_id']<=0){
				$res['msg'] = '请选择商品分类';
			}
			else{
				if($id){
					$Db->update($Base->table('company_product'),$data,array('id'=>$id));
				}
				else{
					if(!$company['is_admin']) $data['c_id'] = $company['p_id'] ? $company['p_id']: $company['id'];
					$Db->insert($Base->table('company_product'),$data);
				}
				$res = array('status'=>'1','msg'=>'提交成功','href'=>'/'.COMPANY_PATH.'/?m=product');
			}
			echo $Json->encode($res);
			exit;
		}
	}

	//上下架商品
	protected function is_not(){
		global $Json;
		global $Db;
		global $Base;
		global $company;
		
		$res['status'] = 0;
		$id = intval($_REQUEST['id']);  //当前操作的product商品id
		
		$product = $this->check_product_exists($id);
		
		if(empty($product)){
			$res['msg'] = '商品ID不存在或不合法！';
		}
		else{
			$data['is_not'] = $product['is_not']==2 ? 1 : 2;
			$Db->update($Base->table('company_product'),$data,array('id'=>$id));
			$res['status'] = 1;
		}
		echo $Json->encode($res);
		exit;
	}

	//删除商品
	protected function delete(){
		global $Json;
		global $Db;
		global $Base;
		global $company;
		
		$res['status'] = 0;
		$id = intval($_REQUEST['id']);  //当前操作的product商品id
		
		$sql = "delete from ".$Base->table('company_product')." where id=$id ";
		if(!$company['is_admin']) $sql .= $company['p_id']? "and c_id=".$company['p_id']."" : "and c_id=".$company['id']."";
		
		$Db->execSql($sql);
		echo $Json->encode(array('status'=>1));
		exit;
	}
	
	//提取商品列表
	private function get_product_list($c_id,$cat_id,$is_not,$page,$pageSize){
		global $Base,$Db,$tp;
		$spm = '';
		$sql = "SELECT p.*,c.name as cname,c.id as cid,ca.name as caname FROM ".$Base->table('company_product')." as p".
			   " LEFT JOIN ".$Base->table('company')." as c ON p.c_id=c.id".
			   " LEFT JOIN ".$Base->table('company_product_cat')." as ca ON p.cat_id=ca.id".
			   " WHERE 1 ";
		
		if($c_id>0){
			$sql .= "and p.c_id=$c_id ";
			$tp->assign('c_id',$c_id);
			$spm .= "&c_id=$c_id"; 
		}
			   
		if($cat_id>0){
			$sql .= "and p.cat_id=$cat_id ";
			$tp->assign('cat_id',$cat_id);
			$spm .= "&cat_id=$cat_id";
		}	   

		if($is_not>0){
			$sql .= " AND p.is_not=$is_not ";
			$tp->assign('is_not',$is_not);
			$spm .= "&is_not=$is_not";
		}
		
		$resCount = count($Db->query($sql));
		$sql .= "order by p.id desc limit ".($page-1)*$pageSize.", $pageSize";
		//echo $sql;
		$res = $Db->query($sql);
		return array('count'=>$resCount,'res'=>$res,'spm'=>$spm);
	}
	
	//提取商家对应的商品分类
	private function get_company_product_cat($c_id){
		global $Base,$Db;
		$sql= "select * from ".$Base->table('company_product_cat')." where cid=$c_id and pid=0 and is_book=1 order by sort asc, id asc";
		$res = $Db->query($sql);
		foreach($res as $k=>$v){
			$sql = "select * from ".$Base->table('company_product_cat')." where pid=".$v['id']." and is_book=1 order by sort asc, id asc";
			$res[$k]['subs'] = $Db->query($sql);
		}
		return $res;
	}
	
	//判断商品是否存在，子类重写实现逻辑判断
	//@param p_id：商品id
	protected abstract function check_product_exists($p_id);
	
}
?>